package com.base.core.exception;


import com.base.core.common.http.response.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 处理业务异常
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<AjaxResult> handleBusinessException(BusinessException e) {
        return ResponseEntity.status(HttpStatus.OK)
                .body(AjaxResult.error(e.getCode(), e.getMessage()));
    }

    // 处理其他异常
    @ExceptionHandler(Exception.class)
    public ResponseEntity<AjaxResult> handleException(Exception e) {
        log.error("系统异常", e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(AjaxResult.error(500, "系统繁忙，请稍后再试"));
    }
}
